<template>
  <div>
    <el-card shadow="hover" body-style="width:100%;" style="margin-bottom: 20px">
      <el-button type="primary" style="margin-bottom: 20px" @click="showDialog">新增店主</el-button>
    <!--    <el-table :data="userList" style="width: 100%" :row-class-name="tableRowClassName">-->
    </el-card>
    <el-card shadow="hover" body-style="width:100%;">
      <el-table
        :data="userList"
        style="width: 100%"
        :row-class-name="tableRowClassName"
        border
      >
        <el-table-column prop="id" label="用户编号" width="180" />
        <el-table-column
          header-align="center"
          align="center"
          prop="username"
          label="用户名"
        />
        <el-table-column
          header-align="center"
          align="center"
          prop="password"
          label="密码"
        />
        <el-table-column
          header-align="center"
          align="center"
          prop="phone"
          label="手机号"
        />
        <el-table-column
          header-align="center"
          align="center"
          prop="email"
          label="邮箱"
        />
        <el-table-column
          header-align="center"
          align="center"
          prop="address"
          label="居住地址"
        />
        <el-table-column
          header-align="center"
          align="center"
          prop="disable"
          label="是否禁用"
        >
          <template slot-scope="scope">
            <el-tooltip :content="'是否禁用该账号？'" placement="top">
              <el-switch
                v-model="scope.row.disable"
                :disabled="scope.row.username === 'admin'"
                active-color="#13ce66"
                inactive-color="#DCDFE6"
                :active-value="'1'"
                :inactive-value="'0'"
                @change="handleChange(scope.row)"
              />
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="prop" label="操作" width="400px" fixed="right">
          <template slot-scope="scope">
            <el-link
              icon="el-icon-edit"
              :underline="false"
              style="margin-right: 10px;color: #14c2c2"
              @click="updateUser(scope.row)"
            >编辑
            </el-link>
            <el-link
              v-if="showDeleteButton(scope.row)"
              icon="el-icon-delete"
              :underline="false"
              style="margin-left: 20px;color: #46a6fe"
              @click="deleteUser(scope.row)"
            >删除
            </el-link>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-pagination
        style="margin-top: 20px; textalign: center"
        :current-page="pageParams.page"
        :page-sizes="[3, 5, 10]"
        :page-size="pageParams.pageSize"
        :pager-count="4"
        layout="prev, pager, next, jumper,->,sizes,total"
        :total="pageParams.total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
      <el-dialog
        :title="userForm.id ? '修改店主' : '新增店主'"
        :visible.sync="dialogFormVisible"
        width="500px"
        height="500px"
      >
        <div style="display: flex; justify-content: center;">
          <el-form ref="ruleForm" :model="userForm" label-width="80px" :rules="rules">
            <el-form-item label="用户名称" prop="username">
              <el-input v-model="userForm.username" :style="{ width: '300px' }" :disabled="userForm.username==='admin'" />
            </el-form-item>
            <el-form-item label="用户密码" prop="password" :style="{ width: '380px' }">
              <el-input v-model="userForm.password" :disabled="userForm.username==='admin'" />
            </el-form-item>
            <el-form-item label="手机号码" prop="phone" :style="{ width: '380px' }">
              <el-input v-model="userForm.phone" :disabled="userForm.username==='admin'" />
            </el-form-item>
            <el-form-item label="电子邮箱" prop="email" :style="{ width: '380px' }">
              <el-input v-model="userForm.email" :disabled="userForm.username==='admin'" />
            </el-form-item>
            <el-form-item label="居住地址" prop="address" :style="{ width: '380px' }">
              <el-input v-model="userForm.address" :disabled="userForm.username==='admin'" />
            </el-form-item>
          </el-form>
        </div>
        <div slot="footer">
          <el-button @click="cancel('ruleForm')">取 消</el-button>
          <el-button type="primary" :disabled="userForm.username==='admin'" @click="submitUser('ruleForm')">确 定</el-button>
        </div>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import row from 'element-ui/packages/row'

export default {
  name: 'AddUser',
  data() {
    return {
      // activeValue: 'true',
      // inactiveValue: 'false',
      dialogFormVisible: false,
      userList: [],
      userForm: { username: '', password: '' },
      page: 1,
      limit: 10,
      total: 0,
      rules: {
        username: [
          { required: true, message: '请输入活动名称', trigger: 'blur' },
          { min: 1, max: 15, message: '长度在 1 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ],
        phone: [{
          required: true,
          message: '请输入手机号',
          trigger: 'change'
        }, {
          pattern: /^1(3|4|5|7|8|9)\d{9}$/,
          message: '手机号格式错误',
          trigger: 'blur'
        }],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'change' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: ['blur', 'change'] }
        ],
        address: [
          { required: true, message: '请输入用户地址', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ]
      },
      // 分页参数
      pageParams: {
        page: 1,
        pageSize: 5,
        total: 0
      }
    }
  },
  computed: {
    row() {
      return row
    }
  },
  mounted() {
    this.getUserList(this.pageParams)
  },
  methods: {
    async handleChange(row) {
      // 处理状态改变
      console.log(row)
      this.$confirm('此操作将修改策略状态, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const userId = {
          id: row.id
        }
        const res = await this.$API.admin.disableUser(userId)
        if (res.code === 200) {
          this.$message({
            type: 'success',
            message: '修改成功'
          })
          this.getUserList()
        }
      }).catch(() => {
        this.getUserList()
        this.$message({
          type: 'info',
          message: '已取消禁用'
        })
      })
    },
    showDeleteButton(row) {
      return row.username !== 'admin'
    },
    tableRowClassName({ row, rowIndex }) {
      console.log('============', row, rowIndex)
      if (rowIndex === 1) {
        return 'warning-row'
      } else if (rowIndex === 3) {
        return 'success-row'
      }
      return ''
    },
    async getUserList() {
      const res = await this.$API.admin.reqUserList(this.pageParams)
      if (res.code === 200) {
        console.log('+++++++++++++++++++++', res.data)
        this.pageParams.total = res.data.total
        this.userList = res.data.userVoList
        console.log(res.data)
      }
    },
    showDialog() {
      // 显示对话框
      this.dialogFormVisible = true
      // 如果点了取消会清空数据，每次打开都会为空
      this.userForm = {
        username: '',
        password: ''
      }
    },
    updateUser(row) {
      this.dialogFormVisible = true
      this.userForm = { ...row }
    },
    // 删除这条信息
    async deleteUser(row) {
      this.$confirm('删除店主后无法回撤, 是否继续?', '删除评论', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          const res = await this.$API.admin.deleteById(row)
          if (res.code === 200) {
            this.$message.success('删除店家成功')
            this.getUserList()
          }
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    async submitUser(ruleForm) {
      this.$refs[ruleForm].validate(async(valid) => {
        if (valid) {
          this.dialogFormVisible = false
          let res
          if (this.userForm.id) {
            res = await this.$API.admin.reqUpdateUser(this.userForm)
          } else {
            res = await this.$API.admin.reqSaveUser(this.userForm)
          }
          if (res.code === 200) {
            // 有id 修改成功 没有id 添加成功
            this.$message.success(
              this.userForm.id ? '修改用户名密码成功' : '添加店主账户成功'
            )
            this.getUserList()
          }
        } else {
          console.log('error submit!!')
          this.$message({
            type: 'warning',
            message: '请输入必选项!!!'
          })
          return false
        }
      })
    },
    cancel(ruleForm) {
      this.$refs[ruleForm].resetFields()
      this.dialogFormVisible = false
      this.userForm = {
        username: '',
        password: ''
      }
    },

    // 分页函数
    handleSizeChange(newPageSize) {
      this.pageParams.pageSize = newPageSize
      this.getUserList()
    },
    handleCurrentChange(current) {
      this.pageParams.page = current
      this.getUserList()
    }

  }

}
</script>

<style scoped>
.el-table .warning-row {
  background-color: oldlace;
}

.el-table .success-row {
  background-color: #f0f9eb;
}
.headerBg {
  background: #eee!important;
}
</style>
